package com.py.credit.controller;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.common.rest.RestServiceResult;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.enums.ResourceBatchLogFieldsEnum;
import com.py.credit.service.DataReminderService;
import com.py.credit.service.RemoteBaseService;
import com.py.framework.core.annotation.IgnoreAuthority;
import com.py.framework.core.common.Page;
import com.py.framework.core.helper.HttpServletHelper;

@RestController
@RequestMapping(value = "/dataReminder")
public class DataReminderController extends BaseController{
	@Autowired
	private RemoteBaseService remoteBaseService;	
	@Autowired
	DataReminderService dataReminderService;
	
	/**
	 * 异议处理提醒定时任务
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/ReminderDisappealTask.do")
	public RestServiceResult<Object> ReminderDisappealTask() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			Object data = dataReminderService.run();
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("DataReminderController#ReminderDisappealTask", "异议处理提醒定时任务异常！ERROR：" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("异议处理提醒定时任务异常！");
			return result;
		}
	}
	/**
	 * 异常处理提醒定时任务
	 * @return
	 */
	@IgnoreAuthority
	@RequestMapping(value = "/ReminderExceptionTask.do")
	public RestServiceResult<Object> ReminderExceptionTask() {
		RestServiceResult<Object> result = new RestServiceResult<Object>();
		try {
			Object data = dataReminderService.ReminderExceptionTask();
			result.setData(data);
			return result;
		} catch (Exception e) {
			ExceptionLogger.error("DataReminderController#ReminderExceptionTask", "异常处理提醒定时任务异常！ERROR：" + e.getMessage(), e);
			result.setCode(RestServiceResult.CODE_UNKNOW);
			result.setMsg("异常处理提醒定时任务异常！");
			return result;
		}
	}
	@RequestMapping(value = "/getexistenceList.json")
	public RestServiceResult<List<Map<String, Object>>> getexistenceList(
			 Page page,String msg_type,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DataReminderController#getexistenceList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}		
		List<Map<String, Object>> tableList = dataReminderService.getByRolenameForPage( page,
				userInfo,msg_type);
		
		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
	}
	
	
	@RequestMapping(value = "/getExceptionList.json")
	public RestServiceResult<List<Map<String, Object>>> getExceptionList(
			 Page page,String msg_type,
			HttpServletRequest request) {
		String token = HttpServletHelper.getUserToken(request);
		RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
		// 获取当前用户信息
		RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
		SysUser userInfo = null;
		if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
			userInfo = user.getData();
			if (null == userInfo || userInfo.getId() <= 0) {
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("系统异常，请稍后再试！");
				ExceptionLogger.error("DataReminderController#getExceptionList", "未获取到当前用户信息！");
				return result;
			}
		} else {
			result.setCode(user.getCode());
			result.setMsg(user.getMsg());
			return result;
		}		
		List<Map<String, Object>> tableList = dataReminderService.getExceptionListForPage( page,
				userInfo,msg_type);
		
		return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
	}
		@IgnoreAuthority
	   @RequestMapping(value = "/updateStatus.json")
	    public RestServiceResult<Object> updateStatus(
	            @RequestParam(value = "type", defaultValue = "", required = true) String type,
	            @RequestParam(value = "id", defaultValue = "", required = true) String id,
	            HttpServletRequest request) {
			RestServiceResult<Object> result = new RestServiceResult<Object>();
		   String tablename ="";
		   if(type.equals("异议处理")) {
			   tablename = "t_datareminder";
		   }else if(type.equals("异常处理")){
			  tablename = "t_exceptionreminder";
		   }else{
			   tablename = "t_feedback";
		   }
		   dataReminderService.updateStatus(tablename, id);
		return result;
	    }
		
		/**
		 * 数据采集反馈定时任务
		 * @return
		 */
		@IgnoreAuthority
		@RequestMapping(value = "/datafeedbackTask.do")
		public RestServiceResult<Object> datafeedbackTask() {
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			try {
				dataReminderService.datafeedbackTask();
				
				return result;
			} catch (Exception e) {
				ExceptionLogger.error("DataReminderController#datafeedbackTask", "数据采集反馈定时任务异常！ERROR：" + e.getMessage(), e);
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("数据采集反馈定时任务异常！");
				return result;
			}
		}
		@IgnoreAuthority
		@RequestMapping(value = "/getNewCount.json")
		public RestServiceResult<Object> getNewCount(	HttpServletRequest request) {			
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			String token = HttpServletHelper.getUserToken(request);
			
			// 获取当前用户信息
			RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("DataReminderController#getExceptionList", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}
			int count =0;
			if(userInfo.getRoleName().equals("系统管理员")) {
				//获取数据采集反馈
				int a =dataReminderService.getfeedbackCount();
				int b =dataReminderService.getexceptionCount(userInfo.getId());
				int c =dataReminderService.getdisapprealCount(userInfo.getRoleName());
				count = a+b+c;
			}else if(userInfo.getRoleName().equals("部门管理员")) {
				int b =dataReminderService.getexceptionCount(userInfo.getId());
				int c =dataReminderService.getdisapprealCount(userInfo.getRoleName());
				count = b+c;
			}else {
				int c =dataReminderService.getdisapprealCount(userInfo.getRoleName());
			}
			
			
			return new RestServiceResult<Object>( count);
		}
		
		@RequestMapping(value = "/getdatafeedbackForPage.json")
		public RestServiceResult<List<Map<String, Object>>> getdatafeedbackForPage(
				 Page page,String msg_type,
				HttpServletRequest request) {
			String token = HttpServletHelper.getUserToken(request);
			RestServiceResult<List<Map<String, Object>>> result = new RestServiceResult<List<Map<String, Object>>>();
			// 获取当前用户信息
			RestServiceResult<SysUser> user = remoteBaseService.getCurrentUser(token);
			SysUser userInfo = null;
			if (RestServiceResult.CODE_SUCCESS == user.getCode()) {
				userInfo = user.getData();
				if (null == userInfo || userInfo.getId() <= 0) {
					result.setCode(RestServiceResult.CODE_UNKNOW);
					result.setMsg("系统异常，请稍后再试！");
					ExceptionLogger.error("DataReminderController#getdatafeedbackForPage", "未获取到当前用户信息！");
					return result;
				}
			} else {
				result.setCode(user.getCode());
				result.setMsg(user.getMsg());
				return result;
			}		
			List<Map<String, Object>> tableList = dataReminderService.getdatafeedbackForPage( page,
					userInfo,msg_type);
			
			return new RestServiceResult<List<Map<String, Object>>>(page.getTotalCount(), tableList);
		}
		
		@IgnoreAuthority
		@RequestMapping(value = "/deletedata.json")
		public RestServiceResult<Object> deletedata(		
				@RequestParam(value = "ids", defaultValue = "", required = true) String ids,
				@RequestParam(value = "type", defaultValue = "", required = true) String type
				) {
			RestServiceResult<Object> result = new RestServiceResult<>();
			List<String> idList = java.util.Arrays.asList(ids.split(","));
			String tablename ="";
			if(type.equals("A")) {
				tablename="t_exceptionreminder";
			}
			if(type.equals("B")) {
				tablename="t_datareminder";
			}
			if(type.equals("C")) {
				tablename="t_feedback";
			}
			try {	
				for(String id:idList) {
					dataReminderService.deletedata(id,tablename);
				}
				
				return result;
			} catch (Exception e) {
				ExceptionLogger.error("DataReminderController#deletedata", "删除数据异常！ERROR:" + e.getMessage(), e);
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("删除数据异常！");
				return result;
			}
		}
		/**
		 * 许可有效更新定时任务
		 * @return
		 */
		@IgnoreAuthority
		@RequestMapping(value = "/XkztUpdateTask.do")
		public RestServiceResult<Object> XkztUpdateTask() {
			RestServiceResult<Object> result = new RestServiceResult<Object>();
			try {
				dataReminderService.XkztUpdateTask();
				
				return result;
			} catch (Exception e) {
				ExceptionLogger.error("DataReminderController#XkztUpdateTask", "许可有效更新定时任务异常！ERROR：" + e.getMessage(), e);
				result.setCode(RestServiceResult.CODE_UNKNOW);
				result.setMsg("许可有效更新定时任务异常！");
				return result;
			}
		}
		
}
